package com.blog.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

public class DataAccess {
	private static DataAccess instance=null; 
	private static String JDriver=null ;
	private static String URL = null;
	private static String User = null;
	private static String Password = null;
	private static Connection conn= null;
	private static String xmlPath="database.conf.xml";
	private static String xsdPath="database.conf.xsd";
	private static HashMap<String,String> hm = new HashMap<String,String>();
	static {
		if(XmlValidator.validate(xsdPath, xmlPath)) {
			hm = XmlParser.parser(xmlPath);
			JDriver = hm.get("driver");
			URL = hm.get("url");
			User = hm.get("user");
			Password = hm.get("password");
		}
	}

	/**
	 * 获取数据库连接
	 * @return conn //返回数据库连接
	 * @throws SQLException 
	 */
	public Connection getConnection() {
		try {
			if(conn==null || conn.isClosed()){
					Class.forName(JDriver);
					conn=DriverManager.getConnection(URL, User, Password);
			}
		} catch (SQLException e) {
			System.err.println("获取数据库连接失败！！！");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			System.err.println("数据库驱动加载失败！！！");
			e.printStackTrace();
		}
		return conn;
	}
	
	
	private DataAccess() {}
	
	/**
	 * 获取DataAccess实例，实现单例
	 * @return instance
	 */
	public static DataAccess getInstance() {
		if(instance==null)
			instance=new DataAccess();
		return instance;
	}
	
	/**
	 * 关闭结果集
	 * @param _rSet
	 */
	public void closeConnection(ResultSet _rSet) {
			try {
				if(_rSet!=null) {
					_rSet.close();
					_rSet = null;
				}
			} catch (SQLException e) {
				System.err.println("关闭连接失败！");
				e.printStackTrace();
			}
	}
	
	/**
	 * 关闭数据库操作
	 * @param _stmt
	 */
	public void closeConnection(Statement _stmt) {
		try {
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}
	}
	
	public void closeConnection(PreparedStatement _stmt) {
		try {
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭数据库连接
	 * @param _conn
	 */
	public void closeConnection(Connection _conn) {
		try {
			if(_conn!=null) {
				_conn.close();
				_conn = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭所有连接
	 * @param _rSet
	 * @param _stmt
	 * @param _conn
	 */
	public void closeConnection(ResultSet _rSet,Statement _stmt,Connection _conn) {
		try {
			if(_rSet!=null) {
				_rSet.close();
				_rSet=null;
			}
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
			if(_conn!=null) {
				_conn.close();
				_conn = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}	
	}
	public void closeConnection(ResultSet _rSet,PreparedStatement _stmt,Connection _conn) {
		try {
			if(_rSet!=null) {
				_rSet.close();
				_rSet=null;
			}
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
			if(_conn!=null) {
				_conn.close();
				_conn = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}	
	}
	
	public void closeConnection(Connection _conn,ResultSet _rSet) {
		try {
			if(_rSet!=null) {
				_rSet.close();
				_rSet=null;
			}
			if(_conn!=null) {
				_conn.close();
				_conn = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}	
	}
	
	
	
	public void closeConnection(Connection _conn,Statement _stmt) {
		try {
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
			if(_conn!=null) {
				_conn.close();
				_conn = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}	
	}
	
	public void closeConnection(Connection _conn,PreparedStatement _stmt) {
		try {
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
			if(_conn!=null) {
				_conn.close();
				_conn = null;
			}
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}	
	}
	
	public void closeConnection(ResultSet _rSet,Statement _stmt) {
		try {
			if(_rSet!=null) {
				_rSet.close();
				_rSet=null;
			}
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
			
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}	
	}
	public void closeConnection(ResultSet _rSet,PreparedStatement _stmt) {
		try {
			if(_rSet!=null) {
				_rSet.close();
				_rSet=null;
			}
			if(_stmt!=null) {
				_stmt.close();
				_stmt = null;
			}
			
		} catch (SQLException e) {
			System.err.println("关闭连接失败！");
			e.printStackTrace();
		}	
	}
	
	
	
	/*
	public ResultSet select(String sql) {
		try {
			conn=getConnection();
			stmt=conn.createStatement();
			rSet=stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			return rSet;
		}
	}
	*/


	
}
